<?php

/**
 * to use this method, please signup a captcha private key & public key at https://www.google.com/recaptcha
 * @see https://www.google.com/recaptcha
 *
 */
class Nuo_Html_Element_Captcha extends Nuo_Html_Element_Field
{
	protected $_name = 'captcha';

	protected $_rules = array();

	protected $_meta = array(
		'label' => 'Captcha',
		'render' => 'captcha',
		'adapter' => 'recaptcha',
	);

	protected $_attrib = array(
		'type' => 'text',
		'class' => 'text email',
		'type' => 'text',
		'name' => 'email',
	);

	protected $_adapter = NULL;

	/**
	 * @return Captcha_Null
	 */
	function getAdapter()
	{
		if (NULL == $this -> _adapter)
		{
			$config = Nuo::getConfig('captcha');

			$adapter = isset($config['adapter']) ? $config['adapter'] : 'image';

			$class_name = 'Captcha_' . ucfirst($adapter);

			$this -> _adapter = new $class_name($config);
		}
		return $this -> _adapter;

	}

	public function _isValid($value)
	{
		$valid = TRUE;

		$adapter = $this -> getAdapter();

		if (!$adapter -> isValid($value))
		{
			$this -> addError(Intl::__('Validate code is not match'));
			$valid = FALSE;
		}

		return $valid;
	}

}
